//
//  HomePageSingleCaseB.m
//  UI_A项目
//
//  Created by dlios on 15-1-19.
//  Copyright (c) 2015年 songCheng. All rights reserved.
//

#import "HomePageSingleCaseB.h"
#import <sqlite3.h>
@implementation HomePageSingleCaseB

+ (instancetype)homePageSingleCaseB
{
    static HomePageSingleCaseB *homePageSCB = nil;
    
    if (homePageSCB == nil) {
        homePageSCB = [[HomePageSingleCaseB alloc] init];
    }
    return homePageSCB;
}
static sqlite3 *db = nil;
- (void)openSJK
{
    if (db != nil) {
        
        NSLog(@"数据库已经打开");
        return;
        
    }
    NSArray *array1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    
    NSString *pathFile = [array1 lastObject];
    
    NSString *sqlitePath = [pathFile stringByAppendingPathComponent:@"HomePage.sqlite"];
    
    NSLog(@"你是个什么东西 = %@", sqlitePath);
    
    int result = sqlite3_open(sqlitePath.UTF8String, &db);
    
    if (result == SQLITE_OK) {
        NSLog(@"数据库打开成功");
        
        NSString *sqlite = @"CREATE TABLE IF NOT EXISTS homePage1 (number INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, title TEXT)";
        
        int result = sqlite3_exec(db, sqlite.UTF8String, NULL, NULL, nil);
            
            if (result == SQLITE_OK) {
                NSLog(@"创建表成功");
            }else
            {
                NSLog(@"创建表失败");
            }
        }
        else
        {
            NSLog(@"数据库打开失败");
        }
    
}

- (void)creatHomePageSCB
{

    NSString *sqlite = @"CREATE TABLE IF NOT EXISTS homePage1 (number INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, title TEXT)";
    
    int result = sqlite3_exec(db, sqlite.UTF8String, NULL, NULL, nil);
    
    if (result == SQLITE_OK) {
        NSLog(@"创建表成功");
    }else
    {
        NSLog(@"创建表失败");
    }

}
- (void)insertHomePageSCB:(HomeModel *)homePageSCB
{
    NSString *insertSql = [NSString stringWithFormat:@"INSERT INTO homePage1(name, title)values('%@', '%@')", homePageSCB.name, homePageSCB.title];
    int result = sqlite3_exec(db, insertSql.UTF8String, NULL, NULL, nil);
    
    if (result == SQLITE_OK) {
        NSLog(@"插入成功");
    }else
    {
        NSLog(@"插入失败");
    }
    
}
- (void)deleteHomePageSCB
{
    NSString *deleteSql = [NSString stringWithFormat:@"DROP TABLE homePage1"];
    
    int result = sqlite3_exec(db, deleteSql.UTF8String, NULL, NULL, nil);
    
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    }else
    {
        NSLog(@"删除失败");
    }

}
- (NSMutableArray *)selectHomePageSCB
{
    NSMutableArray *array = [NSMutableArray array];
    
    NSString *seleSql = @"SELECT * FROM homePage1";
    
    sqlite3_stmt *stmt = nil;
    
    int result = sqlite3_prepare(db, seleSql.UTF8String, -1, &stmt, nil);
    
    if (result == SQLITE_OK) {
        NSLog(@"查询成功");
        
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            
            const unsigned char *name = sqlite3_column_text(stmt, 1);
            
            const unsigned char *title = sqlite3_column_text(stmt, 2);

            NSString *nameStr = [NSString stringWithUTF8String:(const char *)name];
            
            NSString *titleStr = [NSString stringWithUTF8String:(const char *)title];
            
            HomeModel *homeModel = [[HomeModel alloc] init];
            
            homeModel.name = nameStr;
            
            homeModel.title = titleStr;
            
            [array addObject:homeModel];
            
        }
        
    }else{
    
        NSLog(@"查询失败");
    }
    
    return array;
}
@end






























